# -*- coding: UTF-8 -*-
# @Project ：VehicleDimensionMeasurementSystem 
# @File    ：view.py
# @IDE     ：PyCharm 
# @Date    ：2024/6/17 下午10:16
import sys
from PySide6.QtCore import QTime, QDate
from PySide6.QtGui import QAction, Qt
from PySide6.QtWidgets import QApplication, QWidget, QAbstractItemView, QTableWidgetItem

from resources.ui.history_queryUi import Ui_Form
from source.database.db_operation import VehicleRecordQuery
from source.modules.app_function import AppFunction


class HistoryQueryWindow(QWidget, Ui_Form):
    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.sayPrint = None
        self.init_ui()
        self.bind()

    def bind(self):
        self.btnQuery.clicked.connect(lambda: self.query())
        self.sayPrint.triggered.connect(lambda: self.view_report())

    def init_ui(self):
        self.tableWidgetHistoryQuery.setColumnCount(19)
        self.tableWidgetHistoryQuery.setHorizontalHeaderLabels(
            ['ID', '检测时间', "号牌号码", "号牌种类", "流水号", "车辆识别代号", '车辆类型', '测量长', '测量宽',
             '测量高',
             '测量轴距', '测量栏板深度', '测量整备质量', '长判定', '宽判定', '高判定', '轴距判定', '栏板深度判定',
             '重量判定', ])
        self.tableWidgetHistoryQuery.verticalHeader().setVisible(False)  # 开启垂直标题序号
        self.tableWidgetHistoryQuery.setSelectionBehavior(
            QAbstractItemView.SelectionBehavior.SelectRows)  # 设置选择行行为，以行为单位
        self.dateEditStart.setDate(QDate.currentDate())
        self.dateEditEnd.setDate(QDate.currentDate())
        self.sayPrint = QAction('打印')
        self.setContextMenuPolicy(Qt.ContextMenuPolicy.ActionsContextMenu)
        self.addActions([self.sayPrint, ])
        self.query()  # 初始化的时候需要查询

    def query(self):
        # 获取查询的条件
        serial_number = self.leSerialNumber.text().strip()
        plate_number = self.lePlateNumber.text().strip()
        vin = self.leVIN.text().strip()
        start_date = self.dateEditStart.text().strip()
        end_date = self.dateEditEnd.text().strip()
        measure_record = VehicleRecordQuery()
        query_condition_dict = {
            'serial_number': serial_number if serial_number else None,
            'plate_number': plate_number if plate_number else None,
            'vin': vin if vin else None,
            'start_date': start_date if start_date else None,
            'end_date': end_date if end_date else None,
        }
        measure_record_list = measure_record.all_measure_record(**query_condition_dict)
        self.tableWidgetHistoryQuery.setRowCount(len(measure_record_list))
        for row, data_list in enumerate(measure_record_list):
            for column, item in enumerate(data_list):
                if item is None:
                    item = '-'
                elif item is False:
                    item = '不合格'
                elif item is True:
                    item = '合格'
                else:
                    item = str(item)
                self.tableWidgetHistoryQuery.setItem(row, column, QTableWidgetItem(item))
        self.tableWidgetHistoryQuery.resizeColumnsToContents()  # 根据内容自适应宽度

    # 查看报告单
    def view_report(self):
        selected_record = self.tableWidgetHistoryQuery.selectedItems()  # 获取当前行数据
        record_list = [item.text() for item in selected_record]
        AppFunction.print_report(int(record_list[0]))


if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = HistoryQueryWindow()
    window.show()
    app.exec()
